/*
Scripts to create flowText (rectangular) in SVG 1.1 UAs
Copyright (C) <2007>  <Andreas Neumann>
Version 1.0, 2007-02-26
neumann@karto.baug.ethz.ch
http://www.carto.net/
http://www.carto.net/neumann/

----

Documentation: http://www.carto.net/papers/svg/textFlow/

----

current version: 1.0.

1.0 (2007-02-26)
initial release

-------

This ECMA script library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library (lesser_gpl.txt); if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

----

original document site: http://www.carto.net/papers/svg/textFlow/
Please contact the author in case you want to use code or ideas commercially.
If you use this code, please include this copyright header, the included full
LGPL 2.1 text and read the terms provided in the LGPL 2.1 license
(http://www.gnu.org/copyleft/lesser.txt)

-------------------------------

Please report bugs and send improvements to neumann@karto.baug.ethz.ch
If you use this control, please link to the original (http://www.carto.net/papers/svg/textFlow/)
somewhere in the source-code-comment or the "about" of your project and give credits, thanks!

*/
var svgNS = "http://www.w3.org/2000/svg";
//main function
function textFlow2(myText,textToAppend,maxWidth,x,ddy,justified) {
        //split the text at all spaces and dashes
        var words = myText.split(/[\s-]/);
        var line = "";
        var computedTextLength = 0;
        var tspanEl;
        var output = "";

        tspanEl = document.createElementNS(svgNS,"tspan");
        tspanEl.appendChild(document.createTextNode(line));
        textToAppend.appendChild(tspanEl)

        
        for (i=0;i<words.length;i++) {
            var word = words[i];
            line += word + " ";
            tspanEl.firstChild.nodeValue = line;
            computedTextLength = tspanEl.getComputedTextLength();

            if (computedTextLength > maxWidth) {
                var lw = words[i].length + 2; //the 2 is because we also strip off white space
                //si hay mas de una palabra en la linea, quitar la ultima.
                if(line.length > lw){
                    line = line.slice(0,(line.length - lw)); 
                    output = output+line+"\n";
                    tspanEl.firstChild.nodeValue = "";
                    line = words[i] +" ";                        
                }                    
             }
            
            
            if (i == words.length - 1 && line.length > 0) {
                output = output + line.slice(0,line.length-1);
            }
        }

        textToAppend.removeChild(tspanEl)
        return output;
}